﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Net.Model;
using UcAsp.Net.BLL;
namespace UcAsp.Net.Web.Controllers
{
    public class CategoriesController : Controller
    {
        //
        // GET: /Categories/

        public ActionResult Index(string showpath)
        {
            BLL.SettingConfig.Theme(Request.UserAgent);
            Model.Categories categories = new BLL.CategoriesBLL().GetCategoriesByShowPath(showpath);
            if (categories == null)
            {
                try
                {
                    categories = new BLL.CategoriesBLL().GetCategoriesById(int.Parse(showpath));
                }
                catch { }
            }
            List<Model.ArticelsViews> list = new List<ArticelsViews>();
            int pageindex = Convert.ToInt16(Request.QueryString["i"]) - 1 < 0 ? 0 : Convert.ToInt16(Request.QueryString["i"]) - 1;
            ViewData["PageCount"] = 0;
            ViewData["Categories"] = new Model.Categories();
            int pagecount = 0;
            if (categories != null)
            {
                int id = categories.ID;
                ViewData["Categories"] = categories;
                if (categories.ContentTempPath.IndexOf("/", 0) > -1)
                {
                    list = new BLL.ArticlesBLL().SelectViews(id, null, true, null, null, "", 20, pageindex, out pagecount);
                    ViewData["PageCount"] = pagecount;
                }
                else
                {
                    list = new BLL.ArticlesBLL().SelectViews(id, null, true, null, null, "", 20, pageindex, out pagecount);
                    ViewData["PageCount"] = pagecount;
                }
                if (pageindex > pagecount)
                {
                    pageindex = pagecount;
                }
                ViewData["ChildMenu"] = new BLL.CategoriesBLL().GetCategoriessTree(categories.ID);
                return View("~/themes/" + BLL.SettingConfig.GetSiteSetting.Theme + "/Categories/" + categories.TmepPath + ".cshtml", list);
            }
            else
            {
                ViewData["PageCount"] = 0;
                return View(list);
            }
        }
        public ActionResult IndexPage(string showpath,int pageindex)
        {
            BLL.SettingConfig.Theme(Request.UserAgent);
            Model.Categories categories = new BLL.CategoriesBLL().GetCategoriesByShowPath(showpath);
            if (categories == null)
            {
                try
                {
                    categories = new BLL.CategoriesBLL().GetCategoriesById(int.Parse(showpath));
                }
                catch { }
            }
            List<Model.ArticelsViews> list = new List<ArticelsViews>();
            pageindex = pageindex - 1 < 0 ? 0 : pageindex - 1;
            ViewData["PageCount"] = 0;
            ViewData["Categories"] = new Model.Categories();
            int pagecount = 0;
            if (categories != null)
            {
                int id = categories.ID;
                ViewData["Categories"] = categories;
                if (categories.ContentTempPath.IndexOf("/", 0) > -1)
                {
                    list = new BLL.ArticlesBLL().SelectViews(null, id, 20, pageindex, out pagecount);
                    ViewData["PageCount"] = pagecount;
                }
                else
                {
                    list = new BLL.ArticlesBLL().SelectViews(null, id, 20, pageindex, out pagecount);
                    ViewData["PageCount"] = pagecount;
                }
                if (pageindex > pagecount)
                {
                    pageindex = pagecount;
                }
                ViewData["ChildMenu"] = new BLL.CategoriesBLL().GetCategoriessTree(categories.ID);
                return View("~/themes/" + BLL.SettingConfig.GetSiteSetting.Theme + "/Categories/" + categories.TmepPath + ".cshtml", list);
            }
            else
            {
                ViewData["PageCount"] = 0;
                return View(list);
            }
        }
    }
}
